set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/* *******************************************************************
** DATE :
** Writer :
** Memo :
******************************************************************** */
CREATE PROCEDURE [dbo].[TSP_User_GetInfo]
(
	@IsTransaction	tinyint			= 0
,	@ErrorCode		int				= 0		OUTPUT
,	@ErrorMessage	nvarchar( 1000 )= ''	OUTPUT

,	@strUserID	varchar(50)	= NULL	OUTPUT --IN
,	@n4UserNo	int	= NULL	OUTPUT --IN
,	@IsLocalUser	char(1)	= NULL	OUTPUT
,	@strMD5PWD	varchar(100)	= NULL	OUTPUT
,	@strUserName	nvarchar(100)	= NULL	OUTPUT
,	@strPersonalNo	varchar(20)	= NULL	OUTPUT
,	@strEmail	varchar(100)	= NULL	OUTPUT
,	@n1Question	tinyint	= NULL	OUTPUT
,	@strAnswer	nvarchar(200)	= NULL	OUTPUT
,	@strPostNo	varchar(10)	= NULL	OUTPUT
,	@strAddress1	nvarchar(400)	= NULL	OUTPUT
,	@strAddress2	nvarchar(400)	= NULL	OUTPUT
,	@strHomePhone	varchar(20)	= NULL	OUTPUT
,	@strMobilePhone	varchar(20)	= NULL	OUTPUT
,	@strBirthday	varchar(20)	= NULL	OUTPUT
,	@IsLunar	char(1)	= NULL	OUTPUT
,	@IsSMS	char(1)	= NULL	OUTPUT
,	@IsEmail	char(1)	= NULL	OUTPUT
,	@n1JoinMotiveType	tinyint	= NULL	OUTPUT
,	@n1UserLevel	tinyint	= NULL	OUTPUT
,	@n1GroupType	tinyint	= NULL	OUTPUT
,	@dtJoinDate	datetime	= NULL	OUTPUT
,	@dtLastLogin	datetime	= NULL	OUTPUT
,	@strLoginIP	varchar(30)	= NULL	OUTPUT
,	@IsRemoved	tinyint	= NULL	OUTPUT
,	@dtRemoveDate	datetime = NULL	OUTPUT
,	@CodeSex	tinyint	= NULL	OUTPUT
,	@dtModifyDate	datetime = NULL	OUTPUT
)
as
Set NOCOUNT ON

-- Frk Default Value
DECLARE @FrkDateNow DateTime
DECLARE @FrkRowCount int
DECLARE @FrkTranCount int
DECLARE @FrkErrorCode int
DECLARE @FrkErrorMessage nvarchar( 1000 )
Set @FrkDateNow = GetDate()
set @FrkTranCount = @@TRANCOUNT
-- Frk Check


If ( @IsTransaction = 0 )
Begin
	If ( @FrkTranCount < 1 )
	Begin
		Set @FrkErrorCode = -1
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Else If ( @FrkTranCount > 1 )
	Begin
		Set @FrkErrorCode = -2
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
End
Else
Begin
	If ( @FrkTranCount <> 0 )
	Begin
		Set @FrkErrorCode = -3
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Begin Tran
End

-- Implementation

IF ( @strUserID IS NOT NULL )
BEGIN
	SELECT
		@n4UserNo = n4UserNo
	,	@strUserID = strUserID
	,	@IsLocalUser = IsLocalUser
	,	@strMD5PWD = strMD5PWD
	,	@strUserName = strUserName
	,	@strPersonalNo = strPersonalNo
	,	@strEmail = strEmail
	,	@n1Question = n1Question
	,	@strAnswer = strAnswer
	,	@strPostNo = strPostNo
	,	@strAddress1 = strAddress1
	,	@strAddress2 = strAddress2
	,	@strHomePhone = strHomePhone
	,	@strMobilePhone = strMobilePhone
	,	@strBirthday = strBirthday
	,	@IsLunar = IsLunar
	,	@IsSMS = IsSMS
	,	@IsEmail = IsEmail
	,	@n1JoinMotiveType = n1JoinMotiveType
	,	@n1UserLevel = n1UserLevel
	,	@n1GroupType = n1GroupType
	,	@dtJoinDate = dtJoinDate
	,	@dtLastLogin = dtLastLogin
	,	@strLoginIP = strLoginIP
	,	@IsRemoved = IsRemoved
	,	@CodeSex = CodeSex
	,	@dtModifyDate = dtModifyDate
	FROM
		dbo.TB_User WITH (NOLOCK)
	WHERE
		@strUserID = strUserID
END
ELSE IF ( @n4UserNo IS NOT NULL )
BEGIN
	SELECT
		@n4UserNo = n4UserNo
	,	@strUserID = strUserID
	,	@IsLocalUser = IsLocalUser
	,	@strMD5PWD = strMD5PWD
	,	@strUserName = strUserName
	,	@strPersonalNo = strPersonalNo
	,	@strEmail = strEmail
	,	@n1Question = n1Question
	,	@strAnswer = strAnswer
	,	@strPostNo = strPostNo
	,	@strAddress1 = strAddress1
	,	@strAddress2 = strAddress2
	,	@strHomePhone = strHomePhone
	,	@strMobilePhone = strMobilePhone
	,	@strBirthday = strBirthday
	,	@IsLunar = IsLunar
	,	@IsSMS = IsSMS
	,	@IsEmail = IsEmail
	,	@n1JoinMotiveType = n1JoinMotiveType
	,	@n1UserLevel = n1UserLevel
	,	@n1GroupType = n1GroupType
	,	@dtJoinDate = dtJoinDate
	,	@dtLastLogin = dtLastLogin
	,	@strLoginIP = strLoginIP
	,	@IsRemoved = IsRemoved
	,	@CodeSex = CodeSex
	,	@dtModifyDate = dtModifyDate
	FROM
		dbo.TB_User WITH (NOLOCK)
	WHERE
		@n4UserNo = n4UserNo
END

-- Frk Error Check
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount

-- End

COMPLETE:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Commit Tran
		End
	End
	SET @ErrorCode = 0
	SET @ErrorMessage = ''
	return 0
ERROR:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Rollback Tran
		End
	End
	SET @ErrorCode = @FrkErrorCode
	SET @ErrorMessage = @FrkErrorMessage
	return @FrkErrorCode
